//
//  HZHomeOriginalView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/13.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit
import SnapKit

class HZHomeOriginalView: UIView {
    //  原创微博的底部约束
    var originalViewBottonConstraint: Constraint?
    
    
    // 定义一个ViewModel 供外界赋值
    var homeViewModel: HZHomeViewModel?{
        didSet{
            headImageView.HZsetImageWithString(homeViewModel?.homeModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            nameLabel.text = homeViewModel?.homeModel?.user?.name
            // 时间和来源
            timeLabel.text = homeViewModel?.sinaTimeStr
            sourceLabel.text = homeViewModel?.sinaSourceStr
            
            
            
            contentLabel.text = homeViewModel?.homeModel?.text
            membershipImageView.image = homeViewModel?.mbrankImage
            avatarImageView.image = homeViewModel?.verifiedImage
            
            
//            print(homeViewModel?.homeModel?.pic_urls?.count)
            // 卸载约束
            originalViewBottonConstraint?.uninstall()
            // 有配图
            if homeViewModel?.homeModel?.pic_urls?.count > 0 {
                // 赋值
                pictureView.PictureInfo = homeViewModel?.homeModel?.pic_urls
                
                // 设置约束
                self.snp_makeConstraints { (make) in
                    originalViewBottonConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                // 显示配图
                pictureView.hidden = false
            }else{
                // 没有配图   设置约束
                self.snp_makeConstraints { (make) in
                    originalViewBottonConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                // 隐藏配图
                pictureView.hidden = true
            }
        }
    }
    
    
    // MARK - 处理微博时间
    func sinaTimeStr(created_at: String) -> String{
        // 时间格式化
        let df = NSDateFormatter()
        // 设置格式
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        // 本地化
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        // 微博时间
        let sinaDate = df.dateFromString(created_at)!
        // 是否是今年
        let isThisYear = sinaIsThisYear(sinaDate)
        if isThisYear {
            // 今年
            // 日历类
            let calendar = NSCalendar.currentCalendar()
            // 如果是今天
            if calendar.isDateInToday(sinaDate){
                // 秒数  当前时间和微博时间  求差
                let s = Int(NSDate().timeIntervalSinceDate(sinaDate))
                if s < 60 {
                    return "刚刚"
                }else if s > 60 && s < 3600{
                    return "\(s/60)分钟前"
                }else{
                    return "\(s/3600)小时前"
                }
                
            }else if calendar.isDateInYesterday(sinaDate){
                // 如果是昨天
                df.dateFormat = "昨天 HH:mm"
                
            }else{
               // 其他
                df.dateFormat = "MM月dd日 HH:mm"
                
            }
        }else {
            // 不是今年
            // 制定格式
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
            
        }
        return df.stringFromDate(sinaDate)
    }
    
    // 判断是否是今年
    func sinaIsThisYear(sinaDate: NSDate) -> Bool{
        // 时间格式化
        let df = NSDateFormatter()
        // 设置格式
        df.dateFormat = "yyyy"
        // 本地化
        df.locale = NSLocale(localeIdentifier: "en_US")
        // 微博时间字符串
        let sinaTimeStr = df.stringFromDate(sinaDate)
        // 当前时间
        let currentTimeStr = df.stringFromDate(NSDate())
        
        return sinaTimeStr == currentTimeStr
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        // 01
        headImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(10)
            make.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 02
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        // 03
        membershipImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        // 04
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 05
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        //06
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        // 07
        contentLabel.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(10)
        })
        // 08
        pictureView.snp_makeConstraints { (make) in
            // 该约束放到其内部设置
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW - 250)/2)
        }
        
        self.snp_makeConstraints { (make) in
            originalViewBottonConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    }
    
    
    
    
    
    
    
    // MARK - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imageName: "avatar_default")
    // 名字
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    // 用户等级
    private lazy var membershipImageView: UIImageView = UIImageView(imageName: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: AppTheneColor, textFont: 10)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    // 认证 avatar_vgirl
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: HZHomePictureView = HZHomePictureView()
    
}
